package spring;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import web.Posts;
import web.Topic;
import web.Topics;
import web.User;
import web.Users;


@Controller
public class DemoController  {
	//主页
	@RequestMapping("/bbs.do")
	public ModelAndView index(String p,HttpSession s) throws ClassNotFoundException, SQLException{
		ModelAndView m=new ModelAndView();
		int nowpage=1;
		int pagesize=0;
		int g_size=Topic.getSize();
		String g_nowpage=p;
		if(g_nowpage!=null){
			nowpage=Integer.parseInt(g_nowpage);
		}
		if(g_size!=0){
			int size=g_size;
			if(size%10==0){
				pagesize=size/10;
			}else{
				pagesize=size/10+1;
			}
		}
		List<Topics> topics=Topic.getTopics(nowpage);
		m.addObject("topics",topics);
		m.addObject("nowpage",nowpage);
		
		m.addObject("user",s.getAttribute("user"));
		m.addObject("pagesize",pagesize);
		m.setViewName("index");
		return m;
		
	}
	//登录页面
	@RequestMapping("/login.do")
	public String login(){
		return "login";
	}
	//提交登录
	@RequestMapping("/bbs_login.do")
	public String  _login(int uid,String pword,HttpSession s) throws SQLException, ClassNotFoundException{
		User u=Users.getUser(uid);
		String upw=Users.checkUser(uid);
		if(upw!=null&&pword.equals(upw)==true){
			s.setAttribute("user", u);
			return "redirect:bbs.do";
		}else{
			return "redirect:login.do";
		}
		
	}
	//注册页
	@RequestMapping("/register.do")
	public String register(){
		return "register";
	}
	//提交注册
	@RequestMapping("/bbs_register.do")
	public String _register(int uid,String pword,String uname,HttpSession s) throws ClassNotFoundException, SQLException{
		User u=Users.getUser(uid);
		int c_uid=Users.checkUname(uname);
		if(u!=null){
			return "redirect:register.do";
		}else if(c_uid!=0){
			return "redirect:register.do";
		}else{
			Users.addUser(uid, uname, pword);
			User u1=Users.getUser(uid);
			s.setAttribute("user", u1);
			return "redirect:bbs.do";
			
		}
	}
	//帖子页
	@RequestMapping("/post.do")
	public ModelAndView post(int tid,String p,HttpSession s) throws ClassNotFoundException, SQLException{
		ModelAndView m=new ModelAndView();
		int nowpage=1;
		int pagesize=0;
		int g_size=Topic.getPostSize(tid);

		String g_nowpage=p;
		if(g_nowpage!=null){
			nowpage=Integer.parseInt(g_nowpage);
		}
		if(g_size!=0){
			int size=g_size;
			if(size%10==0){
				pagesize=size/10;
			}else{
				pagesize=size/10+1;
			}
		}
		List<Posts> posts=Topic.getPosts(tid,nowpage);
		m.addObject("user",s.getAttribute("user"));
		m.addObject("posts",posts);
		m.addObject("tid",tid);
		m.addObject("nowpage",nowpage);
		m.addObject("pagesize",pagesize);
		m.setViewName("post");
		return m;
	}
	//回复帖子
	@RequestMapping("/post_.do")
	public String _post(int tid,String cont,HttpSession s){
		
		User u=(User)s.getAttribute("user");
		
		if(cont!=null&&!cont.equals("")){
			int uid=u.getUid();
			Topic.reply(uid, tid, cont);
			return "redirect:post.do?tid="+tid+"";
		}else{
			return "redirect:post.do?tid="+tid+"";
		}
		
	}
	
	//提交发帖
	@RequestMapping("/bbs_.do")
	public String _index(String theme,String cont,HttpSession s) throws ClassNotFoundException, SQLException{
		User u=(User)s.getAttribute("user");
		int uid=u.getUid();
		Topic.addTopic(uid, theme);
		int tid=Topic.getMaxTid();
		Topic.reply(uid, tid, cont);
		return "redirect:post.do?tid="+tid+"";
		
		
	}
}